package cn.easytogo.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import net.sf.json.*;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.JSONUtils;
import net.sf.json.xml.XMLSerializer;

public class JSONUtil
{
    private static class DateJsonValueProcessor
        implements JsonValueProcessor
    {

        public Object processArrayValue(Object obj, JsonConfig jsonconfig)
        {
            return process(obj);
        }

        public Object processObjectValue(String s, Object obj, JsonConfig jsonconfig)
        {
            return process(obj);
        }

        private Object process(Object obj)
        {
            if(null != obj)
                return dateFormat.format((Date)obj);
            else
                return null;
        }

        public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
        private DateFormat dateFormat;

        public DateJsonValueProcessor(String s)
        {
            try
            {
                dateFormat = new SimpleDateFormat(s);
            }
            catch(Exception exception)
            {
                dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            }
        }
    }


    private JSONUtil()
    {
    }

    public static String object2json(Object obj)
    {
        return object2json(obj, "yyyy-MM-dd hh:mm:ss");
    }

    public static String object2json(Object obj, String s)
    {
        if(obj == null)
            return null;
        Object obj1;
        if(!(obj instanceof Object[]) && !(obj instanceof Collection)){
	    	obj1 = JSONObject.fromObject(obj, configJson(s));
	    	return ((JSONObject) (obj1)).toString();
    	}else{
	        obj1 = JSONArray.fromObject(obj, configJson(s));
	        return ((JSONArray) (obj1)).toString();
        }
    }

    public static Object json2Object(String s, Class class1)
    {
        JSONObject jsonobject = JSONObject.fromObject(s);
        return JSONObject.toBean(jsonobject, class1);
    }

    public static Object json2Object(String s, Class class1, Map map)
    {
        JSONObject jsonobject = JSONObject.fromObject(s);
        return JSONObject.toBean(jsonobject, class1, map);
    }

    public static Object[] json2ObjectArray(String s, Class class1)
    {
        return json2ObjectArray(s, class1, null);
    }

    public static Object[] json2ObjectArray(String s, Class class1, Map map)
    {
        JSONArray jsonarray = JSONArray.fromObject(s);
        Object aobj[] = new Object[jsonarray.size()];
        for(int i = 0; i < jsonarray.size(); i++)
            aobj[i] = JSONObject.toBean(jsonarray.getJSONObject(i), class1, map);

        return aobj;
    }

    public static List json2ObjectList(String s, Class class1)
    {
        return json2ObjectList(s, class1, null);
    }

    public static List json2ObjectList(String s, Class class1, Map map)
    {
        JSONArray jsonarray = JSONArray.fromObject(s);
        ArrayList arraylist = new ArrayList();
        for(int i = 0; i < jsonarray.size(); i++)
            arraylist.add(JSONObject.toBean(jsonarray.getJSONObject(i), class1, map));

        return arraylist;
    }

    public static String xml2json(String s)
    {
        XMLSerializer xmlserializer = new XMLSerializer();
        xmlserializer.setForceTopLevelObject(true);
        net.sf.json.JSON json = xmlserializer.read(s);
        return json.toString();
    }

    public static String json2xml(String s)
    {
        net.sf.json.JSON json = JSONSerializer.toJSON(s);
        XMLSerializer xmlserializer = new XMLSerializer();
        xmlserializer.setTypeHintsEnabled(false);
        return xmlserializer.write(json);
    }

    private static JsonConfig configJson(String s)
    {
        JsonConfig jsonconfig = new JsonConfig();
        jsonconfig.setIgnoreDefaultExcludes(false);
        jsonconfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
        jsonconfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor(s));
        return jsonconfig;
    }

    public static boolean mayBeJSON(String s)
    {
        return JSONUtils.mayBeJSON(s);
    }

    public static void main(String args[])
    {
    	System.out.println("!!!!"+configJson("11"));
    }
}